<?php
namespace Home\Controller;
use Common\Controller\HomebaseController;
use Home\Service\ClockTimeService;
use Home\Service\WechatService;
// +----------------------------------------------------------------------
// | Date:2017年2月17日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class IndexController extends HomebaseController{
    
    protected $userInfo;
    protected $model;
    protected $orderCode;
    public function _initialize(){
        $userInfo = get_login_user();
        if (empty($userInfo)) $this->ajaxReturn(['info'=>'请先登录！！','code'=>101]);
        $this->userInfo = $userInfo;
        $this->model = M('Order');
        $this->orderCode = [
            '101'=>'公关活动','102'=>'租图租字','103'=>'外发兼职',
            '201'=>'三维后期','202'=>'影视拍摄',
            '301'=>'插画制作','302'=>'人物拍摄','303'=>'静物拍摄','304'=>'三维制作','305'=>'PPT制作',
        ];
    }
    
    public function index(){

        dump($a);
    }
    
    
    public function islogin(){
       
       $this->ajaxReturn(['info'=>'已登录','status'=>1,'code'=>0]);
    }
    
    
    //必传邮箱地址，order_type
    public function submit(){
                
        $post = I('post');
        $email = I('email');
        $ordecode = I('order_code');
        if (empty($ordecode)) $this->error('获取不到正确的订单类型编码order_code'); 

        //图片生成
        if (isset($post['file'])){
            $post['file'] = json_encode($post['file']);
        }
        if (!$post['order_title']) $this->error('请输入订单标题');
        //数据添加
        $order_sn = uniqid();
        $post['create_time'] = date('Y-m-d H:i:s');
        $post['user_name'] = $this->userInfo['name'];
        $post['user_sn'] = $this->userInfo['sn'];
        $post['order_code'] = $ordecode;
        $post['order_sn']=$order_sn;
        $post['supplier_email'] = $email;//供应商邮箱
        if (isset($post['expand']) && $post['expand']){
            $post['expand'] = json_encode($post['expand']);
        }
        $orderid = $this->model->add($post);
        if (!$orderid) $this->error('订单添加出错！！');
        //生成详情页链接
        $detial = sp_get_host().dirname(__ROOT__)."/{$post['order_code']}.html?sn=$order_sn";
        $data['test']['detail']=$detial;
        $data['test']['info']=U('public/detail',['sn'=>$order_sn]);
        
        $data['info']='订单添加成功';
        
        //有邮箱参数进来，才发送邮件
        if (empty($email) && C('SUP_DEBUG')) {
            $email = $this->userInfo['email'];
        }
        $orderTypeName = $this->orderCode[$ordecode];
        
        $subject = "[因赛集团]项目报价需求-{$post['order_title']}";
        $message = "需求详细内容请点击链接:{$detial}";//邮件正文
        $emailsend = sp_send_email($email, $subject, $message);
        if ($emailsend['error'] == 0) {
            $data['info'] .=', 并且成功发送了邮件';
        }else{
            $data['info'] .=', 但是邮件发送失败:'.$emailsend['message'];
        }
        $this->ajaxReturn($data);
        
    }
    

    

    //上传附件
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类   
        $upload->maxSize   =     0 ;// 设置附件上传大小    
        $upload->exts      =     array();// 设置附件上传类型    
//         $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','mov','mp4');// 设置附件上传类型    
        $upload->savePath  =      ''; // 设置附件上传目录    
        $info   =   $upload->upload(); 
        if(!$info) {// 上传错误提示错误信息
            $data['info'] = $upload->getError();
            $this->ajaxReturn($data);
//             $this->error($upload->getError());    
        }else{// 上传成功    
            $data['path'] = '/app/Uploads/'.$info['Filedata']['savepath'].$info['Filedata']['savename'];
            $data['ext'] = $info['Filedata']['ext'];
            $data['name'] = $info['Filedata']['name'];
            $this->ajaxReturn($data);
        }
    }
    

}